package com.zzx.bms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zzx.bms.domain.BookExample;
import com.zzx.bms.domain.User;
import com.zzx.bms.domain.UserExample;
import com.zzx.bms.persistence.UserMapper;
import com.zzx.bms.service.IUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 
* @ClassName: UserServiceImpl 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author Admin
* @date 2015年4月20日 下午9:41:40 
*
 */
@Service(value="userService")
public class UserServiceImpl implements IUserService {

    /** 
    * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么) 
    */ 
    
    private static final long serialVersionUID = 7522721238681998540L;
    
    @Autowired
    private UserMapper userMapper;

    /**
     * 根据用户名 密码查询用户是否存在 用于登录
     */
	public User findUser(String username, String pwd) {
		UserExample exp = new UserExample();
		UserExample.Criteria crt = exp.createCriteria();
		crt.andUsernameEqualTo(username);
		crt.andPasswordEqualTo(pwd);
		List<User> list = userMapper.selectByExample(exp);
		if(list!=null && list.size()>0)
			return list.get(0);
		else
			return null;
	}

	@Override
	public User findUserById(String id) {
		User user = null;
		if(StringUtils.isNotEmpty(id))
			user = userMapper.selectByPrimaryKey(Integer.parseInt(id));
		return user;
	}

	@Override
	public int checkUser(User user) {
		UserExample exp = new UserExample();
		UserExample.Criteria crt = exp.createCriteria();
		if(StringUtils.isNotEmpty(user.getNumber()))
			crt.andNumberEqualTo(user.getNumber().trim());
		if(StringUtils.isNotEmpty(user.getRealname()))
			crt.andRealnameEqualTo(user.getRealname().trim());
		if(StringUtils.isNotEmpty(user.getUsername()))
			crt.andUsernameEqualTo(user.getUsername().trim());
		if(user.getId()!=null)
			crt.andIdNotEqualTo(user.getId());
		crt.andIsDeleteEqualTo("0");
		int flag = userMapper.countByExample(exp);
		return flag;
	}

	@Override
	public int saveUser(User user) {
		int flag = 0;
		if(user.getId()!=null){
			user.setCreateTime(new Date());
			flag = userMapper.updateByPrimaryKeySelective(user);
		}
		else{
			user.setCreateTime(new Date());
			user.setIsDelete("0");
			flag = userMapper.insertSelective(user);
		}
		return flag;
	}

	@Override
	public List<User> findUserList(User user) {
		UserExample exp = new UserExample();
		UserExample.Criteria crt = exp.createCriteria();
		crt.andIsDeleteEqualTo("0");
		exp.setOrderByClause("  create_time  deSC ");
		List<User> list = userMapper.selectByExample(exp);
		return list;
	}

	@Override
	public int deleteUserById(String id) {
		int flag = 0;
		UserExample exp = new UserExample();
		UserExample.Criteria crt = exp.createCriteria();
		User user = userMapper.selectByPrimaryKey(Integer.parseInt(id));
		if(user!=null){
			user.setIsDelete("1");
			flag = userMapper.updateByPrimaryKeySelective(user);
		}

		return flag;
	}

	@Override
	public User checkUserByNum(User user) {
		UserExample exp = new UserExample();
		UserExample.Criteria crt = exp.createCriteria();
		if(StringUtils.isNotEmpty(user.getNumber()))
			crt.andNumberEqualTo(user.getNumber().trim());
		if(StringUtils.isNotEmpty(user.getRealname()))
			crt.andRealnameEqualTo(user.getRealname().trim());
		if(StringUtils.isNotEmpty(user.getUsername()))
			crt.andUsernameEqualTo(user.getUsername().trim());
		if(user.getId()!=null)
			crt.andIdNotEqualTo(user.getId());
		crt.andIsDeleteEqualTo("0");
		List<User> userlist = userMapper.selectByExample(exp);
		if(userlist !=null && userlist.size()>0)
			return userlist.get(0);
		else return null;
	}

	@Override
	public int getUserTotal() {
		UserExample exp = new UserExample();
		UserExample.Criteria crt = exp.createCriteria();
		crt.andIsDeleteEqualTo("0");
		int flag = userMapper.countByExample(exp);
		return flag;
	}

}
